<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/taglib_include.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, target-densitydpi=medium-dpi, initial-scale=0.5, minimum-scale=0.5, maximum-scale=0.5, user-scalable=no" />
<meta content="yes" name="apple-mobile-web-app-capable" />
<meta content="black" name="apple-mobile-web-app-status-bar-style" />
<meta content="telephone=no" name="format-detection" />
<link href="${ctx}/css/main.css?ver=${main_css_version}" rel="stylesheet" type="text/css" />
<title>${projectTitle }</title>
</head>
<body>
  <header id="main-top">
    <a href="javascript:void(0);" id="back">
      <div id="main-top-left">
        <img src="${ctx}/images/mui_title_back_btn.png" class="img-2">
      </div>
    </a>
    <div id="main-top-title">添加新乘客</div>
    <div id="main-top-right" style="cursor:pointer" addpassenger="addpassenger">完成</div>
  </header>
  <div id="container">
    <div id="login">
      <ul>
        <li class="registered"><label>真实姓名</label><input name="fullname" id="fullname" type="text"></li>
        <li id="genderfield" class="registered"><label>性别</label>
          <select name="gender" id="gender">
             <c:forEach items="${gendermap}" var="item">
                <option value="${item.key}">${item.value}</option>
            </c:forEach>
          </select>
        </li>
        <li class="registered" id="identificationTypefield"><label>证件类型</label>
          <select name="identificationType" id="identificationType">
            <c:forEach items="${identificationTypeMap}" var="item">
                <option value="${item.key}">${item.value}</option>
            </c:forEach>
          </select>  
        </li>
        <li class="registered" id="identificationNumberfield"><label>证件号码</label><input name="identificationNumber" id="identificationNumber" type="text"></li>
        <li id="mobilefield" class="registered"><label>手机号码</label><input name="mobile" id="mobile" type="text"></li>
        <li class="registered"><label>乘客类型</label>
          <select name="passengerType" id="passengerType">
            <c:forEach items="${passengerTypeMap}" var="item">
                <option value="${item.key}">${item.value}</option>
            </c:forEach>
          </select>
        </li>
      </ul>
    </div>
    <form action="" method="get" id="form">
      <!-- 隐藏表单 -->
      <input type="hidden" name="schType" value="${passengerinfo.schType}" id="schType">
      <input type="hidden" name="regBusNo" value="${passengerinfo.regBusNo}" id="regBusNo">
      <input type="hidden" name="busTypeName" value="${passengerinfo.busTypeName}" id="busTypeName">
      <input type="hidden" name="startStationName" value="${passengerinfo.startStationName}" id="startStationName">
      <input type="hidden" name="arriveStationName" value="${passengerinfo.arriveStationName}" id="arriveStationName">
      <input type="hidden" name="leaveTime" value="${passengerinfo.leaveTime}" id="leaveTime">
      <input type="hidden" name="leaveDate" value="${passengerinfo.leaveDate}" id="leaveDate">
      <input type="hidden" name="orderId" value="${passengerinfo.orderId}" id="orderId">
      <input type="hidden" name="serviceprice" value="${passengerinfo.serviceprice}" id="serviceprice">
      <input type="hidden" name="totalprices" value="${passengerinfo.totalprices}" id="totalprices">
      <input type="hidden" name="fullTicketNum" value="${passengerinfo.fullTicketNum}" id="fullTicketNum">
      <input type="hidden" name="halfTicketNum" value="${passengerinfo.halfTicketNum}" id="halfTicketNum">
      <input type="hidden" name="freeTicketNum" value="${passengerinfo.freeTicketNum}" id="freeTicketNum">
      <input type="hidden" name="price" value="${passengerinfo.price}" id="price">
      <input type="hidden" name="halfPrice" value="${passengerinfo.halfPrice}" id="halfPrice">
      <input type="hidden" name="orderpnr" value="${passengerinfo.orderpnr}" id="orderpnr">
      <input type="hidden" name="startstationId" value="${passengerinfo.startstationId}" id="startstationId">
      <input type="hidden" name="lineTypeId" id="lineTypeId" value="${passengerinfo.lineTypeId}"/>
      <input type="hidden" name="hasPrivilege" id="hasPrivilege" value="${passengerinfo.hasPrivilege}"/>
      <input type="hidden" name="totalPrivilegeAmount" id="totalPrivilegeAmount" value="${passengerinfo.totalPrivilegeAmount}"/>
     </form>
  </div>
  <%@ include file="/WEB-INF/views/popup_alert.jsp"%>
  <%@ include file="/WEB-INF/views/loading.jsp"%>
  <script type="text/javascript" src="${ctx}/js/jquery.min.js?ver=${jquery_min_version}"></script>
  <!-- mergeTo:${ctx}/js/custom.min.js?ver=${custom_min_version} -->
  <script type="text/javascript" src="${ctx}/js/custom.js?ver=${custom_version}"></script>
  <!-- mergeTo -->
  <script type="text/javascript">
      var reg = /^[\u4E00-\u9FA5]{1}\d{8}$/; //匹配军官证
      var passengerType;
      var child = ${constant.passengerType_child};
      $(function(){

    	  $("div[addpassenger='addpassenger']").bind("click",function(){
    		  var fullname = $("#fullname").val();
              var gender = $("#gender").val();
              var identificationType = $("#identificationType").val();
              var identificationNumber = $("#identificationNumber").val();
              var mobile = $("#mobile").val();
              passengerType = $("#passengerType").val();  
              var idcard = ${constant.identificationType_idcard};
              var officercard = ${constant.identificationType_officercard};

              if(passengerType == child){
	            	  if(fullname == ""){
	                      openAlert("姓名不能为空");
	                      return false;
	                  }
	            	  if(mobile == ""){
	                      openAlert("手机号码不能为空");
	                      return false;
	                  }
	            	  
	            	  if(!/^\d{11}$/i.test(mobile)){
	                      openAlert("手机号码输入有误");
	                      return false;
	                  }
	            	  
	            	  identificationType = "";
	            	  identificationNumber = "";
             } else {
                 if(fullname == ""){
                     openAlert("姓名不能为空");
                     return false;
                 }
                 if(identificationNumber == ""){
                     openAlert("证件号码不能为空");
                     return false;
                 }
                 if(mobile == ""){
                     openAlert("手机号码不能为空");
                     return false;
                 }
                 //身份证号码验证
                 if(identificationType == idcard){
                     if(identificationNumber.indexOf(" ")==-1 && identificationNumber.indexOf("　")==-1){
                         identificationNumber = identificationNumber.replace(/\s+/g,"");
                         var checkFlag = new clsIDCard(identificationNumber);
                         var flag = checkFlag.IsValid();
                         if(!flag){
                             openAlert("身份证号码输入有误！");
                             return false;
                         }
                     }else{
                         openAlert("身份证号码输入不能包含空格！");
                         return false;
                     }

                 }
                 //军官证件号码验证
                 if(identificationType == officercard){
                     if(!reg.test(identificationNumber)){ 
                         openAlert("军官证输入有误");
                         return false; 
                     } 
                 }
                 if(!/^\d{11}$/i.test(mobile)){
                      openAlert("手机号码输入有误");
                      return false;
                 }
             }

              $.ajax({
                  type: "POST",
                  url:"${ctx}/passenger/add.do",
                  data:{fullname:fullname,
                	    gender:gender,
                	    identificationType:identificationType,
                        identificationNumber:identificationNumber,
                        mobile:mobile,
                        passengerType:passengerType
                        },
                  success:function (data){         
                      if(data.errcode == 0){
                          $("#form").attr("action","${ctx}/passenger/list.do").submit();
                      }else if(data.errcode == 8){
                          openAlert("手机号码无效");
                      }else if(data.errcode == 9){
                          openAlert("证件号已被使用");
                      }else if(data.errcode == 11){
                          openAlert("必填项未输入");
                      }
                  }
              });
    	  });
    	  //返回按钮绑定单击事件
    	  $("#back").bind("click",function(e){
    		  $("#form").attr("action","${ctx}/passenger/list.do").submit();
          });
    	  //乘客类型下拉框绑定事件
    	  $("#passengerType").bind("change",function(e){
    		  passengerType = $("#passengerType").val(); 
    		  if(passengerType == child){
    			  $("#identificationTypefield").css("display","none");
    			  $("#identificationNumberfield").css("display","none");
    			  $("#genderfield").removeAttr("class");
    		  }else{
    			  $("#identificationTypefield").css("display","block");
                  $("#identificationNumberfield").css("display","block");
                  $("#genderfield").attr("class","registered");
    		  }
    	  });
      });
  </script>
</body>
</html>